package com.quwan.manager.controller;

import com.quwan.common.BaseController;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TVipRights;
import com.quwan.manager.service.IVipRightsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author laozhang
 * @date 2022/4/24
 */
@Api(tags = "后台会员配置——权益配置管理接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("mgr/right")
public class TVipRightController extends BaseController {

    private final IVipRightsService vipRightsService;


    @ApiOperation(value = "权益配置列表分页",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("VipRightsListPage")
    public ApiResult<PageResult<TVipRights>> VipRightsListPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @ApiParam(value="类型",required = false) @RequestParam(required = false) String name) {
        PageResult<TVipRights> res = vipRightsService.getTVipRightsListPage(page, size, name);
        return ApiResult.success(res);
    }

    @ApiOperation(value = "权益配置列表",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("VipRightsList")
    public ApiResult<List<TVipRights>> VipRightsList(
            @ApiParam(value="类型",required = false) @RequestParam(required = false) String name) {
        List<TVipRights> res = vipRightsService.getVipRightsList(name);
        return ApiResult.success(res);
    }

    @ApiOperation("权益配置修改——不需要传时间字段")
    @PostMapping("updateVipRights")
    public ApiResult updateVipRights(TVipRights vipRights){
        boolean success = vipRightsService.updateTVipRights(vipRights);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("addVipRights")
    @ApiOperation("添加权益配置——不需要传时间字段")
    public ApiResult addVipRights(TVipRights vipRights){
        vipRights.setAddTime(LocalDateTime.now());
        boolean success = vipRightsService.addTVipRights(vipRights);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @ApiOperation(value = "权益配置详情",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("detailVipRights")
    public ApiResult detailVipRights(@RequestParam(value = "id") Integer id) {
        TVipRights VipRights = vipRightsService.detailTVipRights(id);
        return ApiResult.success(VipRights);

    }

    @PostMapping("delVipRights")
    @ApiOperation("删除权益配置")
    public ApiResult delVipRights(@RequestParam(value = "id") Integer id){
        boolean success = vipRightsService.delTVipRights(id);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }
}
